//
//  PosterDetailViewController.m
//  DayByDay
//
//  Created by dllo on 16/5/14.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "PosterDetailViewController.h"
#import <AFNetworking.h>
#import "PosterDetailModel.h"
#import <UIImageView+WebCache.h>
#import "DetailTableViewCell.h"
#import "NewSongsDetailsModel.h"
#import "HotListDetailModel.h"

@interface PosterDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)PosterDetailModel *detailModel;
@property (nonatomic, strong)NewSongsDetailsModel *nsDetailModel;
@property (nonatomic, assign)CGFloat *tableViewContentOffSet;
@property (nonatomic, assign)CGFloat currentOffSit_y;
@property (nonatomic, strong)JHJTabBarController *tabBar;

@end

@implementation PosterDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.array = [NSMutableArray array];
    [self createData];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self createView];
//    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.alpha = 0;
//    self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
//    self.automaticallyAdjustsScrollViewInsets = NO;
//    self.navigationController.navigationBar.hidden = YES;
    AppDelegate *tempAppdelegate = [[UIApplication sharedApplication] delegate];
    self.tabBar = tempAppdelegate.JHJTabBar;
   
    self.navigationController.navigationBar.alpha = 0.1;

}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.tabBar showTabBarWithAnimation:NO];
}

- (void)createData {
    NSString *urlString;
    if (self.model) {
        NSString *firstString =@"http://api.songlist.ttpod.com/songlists/";
        NSString *lastString = @"?app=ttpod&v=v9.0.1.2016050614&user_id=0&mid=&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=5e6106cc8d5bcf35f60a2997c79a00bb5a7c631f&idfa=ABDADD65-0877-4840-B14F-DFC8D1114AB0&utdid=VzR0dwnWYs8DANbYJ8SsFzOU&alf=(null)&bundle_id=com.ttpod.music&latitude=-1&longtitude=-1&access_token=(null)&vc=9000100";
        urlString = [NSString stringWithFormat:@"%@%@%@",firstString, self.model.actionModel.value, lastString];
    } else if (self.value) {
        NSString *firstString = @"http://api.dongting.com/song/album/";
        NSString *lastString = @"?app=ttpod&v=v9.0.1.2016050614&user_id=0&mid=iPhone7%2C2&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=5e6106cc8d5bcf35f60a2997c79a00bb5a7c631f&idfa=ABDADD65-0877-4840-B14F-DFC8D1114AB0&utdid=VzR0dwnWYs8DANbYJ8SsFzOU&alf=(null)&bundle_id=com.ttpod.music&latitude=-1&longtitude=-1&access_token=(null)&vc=9000100";
        urlString = [NSString stringWithFormat:@"%@%@%@",firstString, self.value, lastString];
        
    } else if (self._id) {
        NSString *firstString =@"http://api.songlist.ttpod.com/songlists/";
        NSString *lastString = @"?app=ttpod&v=v9.0.1.2016050614&user_id=0&mid=&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=5e6106cc8d5bcf35f60a2997c79a00bb5a7c631f&idfa=ABDADD65-0877-4840-B14F-DFC8D1114AB0&utdid=VzR0dwnWYs8DANbYJ8SsFzOU&alf=(null)&bundle_id=com.ttpod.music&latitude=-1&longtitude=-1&access_token=(null)&vc=9000100";
        urlString = [NSString stringWithFormat:@"%@%@%@",firstString, self._id, lastString];
    } else if (self.valueStr) {
        NSString *firstString =@"http://api.songlist.ttpod.com/songlists/";
        NSString *lastString = @"?app=ttpod&v=v9.0.1.2016050614&user_id=0&mid=&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=5e6106cc8d5bcf35f60a2997c79a00bb5a7c631f&idfa=ABDADD65-0877-4840-B14F-DFC8D1114AB0&utdid=VzR0dwnWYs8DANbYJ8SsFzOU&alf=(null)&bundle_id=com.ttpod.music&latitude=-1&longtitude=-1&access_token=(null)&vc=9000100";
        urlString = [NSString stringWithFormat:@"%@%@%@",firstString, self.valueStr, lastString];
    }
//    NSString *urlString = [NSString stringWithFormat:@"%@%@%@",firstString, self.model.actionModel.value, lastString];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (self.model || self._id || self.valueStr) {
            self.detailModel = [[PosterDetailModel alloc] init];
            [self.detailModel setValuesForKeysWithDictionary:responseObject];
             NSLog(@"kjsdfsdf");
            [[NSNotificationCenter defaultCenter] postNotificationName:@"songs" object:self.detailModel.songs];
        } else if (self.value) {
            self.nsDetailModel = [NewSongsDetailsModel new];
            [self.nsDetailModel setValuesForKeysWithDictionary:[responseObject valueForKey:@"data"]];
            NSLog(@"kjsdfsdf");
            [[NSNotificationCenter defaultCenter] postNotificationName:@"songs" object:self.nsDetailModel.songList];
//            NSLog(@"%@",self.nsDetailModel.songList);
            
        }
        UIImageView *imageV = [self.view viewWithTag:2500];
        if (self.model ) {
            [imageV sd_setImageWithURL:[NSURL URLWithString:self.model.picUrl]];
        } else if (self.value) {
            [imageV sd_setImageWithURL:[NSURL URLWithString:self.nsDetailModel.picUrl]];
        } else if (self._id) {
            [imageV sd_setImageWithURL:[NSURL URLWithString:self.detailModel.detailImageModel.pic]];
            NSLog(@"%@",self.detailModel.detailImageModel.pic);
        } else if (self.valueStr) {
            [imageV sd_setImageWithURL:[NSURL URLWithString:self.detailModel.detailImageModel.pic]];
        }

        [self.tableView reloadData];
        self.tableView.contentOffset = CGPointMake(0, -HEIGHT / 3);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (void)createView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.contentInset = UIEdgeInsetsMake(HEIGHT / 3, 0, 0, 0);
    self.currentOffSit_y = -HEIGHT / 3;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -HEIGHT / 3, WIDTH, HEIGHT / 3)];
    [self.tableView addSubview:imageView];
    self.tableView.contentOffset = CGPointMake(0, -HEIGHT / 3);
    imageView.tag = 2500;
    imageView.backgroundColor = [UIColor greenColor];
    if (self.model) {
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.model.picUrl]];
    } else if (self.value) {
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.nsDetailModel.picUrl]];
    } if (self._id || self.valueStr) {
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.detailModel.detailImageModel.pic]];
    }
    
    [self.tableView registerClass:[DetailTableViewCell class] forCellReuseIdentifier:NSStringFromClass([DetailTableViewCell class])];
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:self.tableView.frame];
    bgImageView.image = [UIImage imageNamed:@"preview"];
    self.tableView.backgroundView = bgImageView;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//    [self setHeaderViewForSectionWithView];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
    view.backgroundColor = [UIColor greenColor];
    view.tag = 20000;
    return view;
}

//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//    return @"dsf";
//}


//- (UIView *)setHeaderViewForSectionWithView {
//    
//}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.model || self._id || self.valueStr) {
        return self.detailModel.songs.count;
    } else if (self.value) {
        return self.nsDetailModel.songList.count;
    } else if (self._id) {
        
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([DetailTableViewCell class])];
    if (self.detailModel || self._id) {
        cell.model = self.detailModel.songs[indexPath.row];
        cell.numberLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    } else if (self.value) {
        cell.model = self.nsDetailModel.songList[indexPath.row];
        cell.numberLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"playMusic" object:indexPath];
    //通知中心JHJTabBarControllerCell;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"rotationImage" object:nil];
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    NSLog(@"%f",self.tableView.contentOffset.y);
    if (scrollView == self.tableView)
    {
        NSLog(@"%f",scrollView.contentSize.height);
        if (scrollView.contentSize.height > 700) {
            
        
        if (scrollView.contentOffset.y > self.currentOffSit_y) {
            [self.tabBar hiddenTabBarWithAnimation:YES];
            self.currentOffSit_y = scrollView.contentOffset.y;
                            if (scrollView.contentOffset.y > scrollView.contentSize.height - HEIGHT * 4 / 3 - 50) {
                                scrollView.bounces = NO;
                            }
        }else if (scrollView.contentOffset.y <= self.currentOffSit_y) {
            [self.tabBar showTabBarWithAnimation:YES];
            self.currentOffSit_y = scrollView.contentOffset.y;
            scrollView.bounces = YES;
        }
        }
        
        if (-scrollView.contentOffset.y < HEIGHT / 3) {
            //        self.currentOffSit_y = scrollView.contentOffset.y;
            if (-scrollView.contentOffset.y < 64) {
                return;
            }
            
//            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
            [self performSelector:@selector(changeContentInset:) withObject:scrollView afterDelay:0 inModes:@[NSRunLoopCommonModes]];
            self.navigationController.navigationBar.alpha = 0.1 * (HEIGHT / 3 + scrollView.contentOffset.y) / 20;
        }
        if (-scrollView.contentOffset.y > HEIGHT / 3) {
            UIImageView *imageVIew = [self.view viewWithTag:2500];
            imageVIew.frame = CGRectMake(-(WIDTH * (-scrollView.contentOffset.y ) / HEIGHT * 3 - WIDTH) / 2, scrollView.contentOffset.y, WIDTH * (-scrollView.contentOffset.y ) / HEIGHT * 3, -scrollView.contentOffset.y);
        }
//        if (scrollView.contentOffset.y > self.currentOffSit_y) {
//            [self.tabBar hiddenTabBarWithAnimation:YES];
//            self.currentOffSit_y = scrollView.contentOffset.y;
//        }
//        if (scrollView.contentOffset.y <= self.currentOffSit_y) {
//            [self.tabBar showTabBarWithAnimation:YES];
//            self.currentOffSit_y = scrollView.contentOffset.y;
//        }
//        self.currentOffSit_y = scrollView.contentOffset.y;
        NSLog(@"%f",self.currentOffSit_y);
        NSLog(@"========%f",scrollView.contentOffset.y);
        
    }
}

- (void)changeContentInset:(UIScrollView *)scrollView {
    [UIView animateWithDuration:0.1 animations:^{
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
//        UIView *view = [self.view viewWithTag:20000];
//        view.frame = CGRectMake(0, -64, WIDTH, 50);
        
    } completion:^(BOOL finished) {
        
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
